<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>图片管理</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <link rel="stylesheet" href="css/iconfont.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/companydata.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
	    <section class="AllContent" style="background-color: #fff;">
	    	<div id="piclist">
	    		<img style="width: 40%;margin:20px auto;display: block;" src="images/loading5.gif">
	    	</div>
	    </section>
	    
	    <!-- 分页区 -->
		<div class="pull-right padder">
            <ul class="pagination " id="pageUl">
            </ul>
        </div>
        
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript" src="js/bootstrap-paginator.js" ></script>
	<script type="text/javascript">
	    mui.init();
		$(function () {
			$('.AllHeader li:eq(1)').text('图片管理');
			$('.AllHeader li:eq(2)').html('<a id="addpicid"><img style="width:22px" src="images/addproducts.png"></a>');
			
	        document.addEventListener('deviceready', onDeviceReady, true);
	        var target_url = getServerUrl()+'/cs/client_image.html?token='+getToken();
	        $("#addpicid").attr("href",target_url);
	        loadPic();
	    });
	    var productlist = false;
	    function onDeviceReady() {
	        document.addEventListener("backbutton", onBackKeyDown, false);
	    }
	    function onBackKeyDown() {
	        openUrl("index.html");
	    }
	    function queryPic() {
	        var q = $("#query").val();
	        var groupname = $("#s_groupname").val();
	        var  pic= false;
	        if(empty(q) && groupname == "全部"){
	            loadPic();
	        }else{
	            $.ajax({
	                type: "post",
	                url: getServerUrl() + "/Open/Company/getpic",
	                dataType: 'json',
	                data: 'access_token=' + getToken()+'&title='+q+'&groupname='+groupname,
	                success: function (json) {
	                    piclist = json.piclist;
	                    showPic(piclist);
	                    showpage(json.total_results,json.page_no,json.page_size,getwindowhref());
	                },
	                error: function () {
	                    alertMessage("请检查网络连接！");
	                }
	            });
	        }
	    }
	    function addpic() {
	        var title = $("#title").val();
	        var image =$("#pic_url").val();
	        var url = $("#url").val();
	        var groupname =$("#groupname").val();
	        if (empty(title)) {
	            alertMessage("图片标题不能为空！");
	            return;
	        }
	        var datas = {access_token:getToken(),title:title,image:image,groupname:groupname,url:url};
	        $.ajax({
	            type: "post",
	            url: getServerUrl() + "/Open/Company/addpic",
	            dataType: 'json',
	            data: datas,
	            success: function (json) {
	                if (json.result == "1") {
	                    var chtml = '';
	                     
						chtml = chtml + '<div class="ProductsListContent">';
		            	chtml = chtml + '<div class="ProductsDetails">';
		    			chtml = chtml + '<ul>';
		    			chtml = chtml + '<li><img width="100%" src="' +image + '"></li>';
				    	chtml = chtml + '<li>';
				    	chtml = chtml + '<p>' + title + '</p>';
				    	chtml = chtml + '<p>' + groupname + '</p>';
				    	chtml = chtml + '</li>';
		    			chtml = chtml + '</ul>';
		    			chtml = chtml + '</div>';
		    			
		    			chtml = chtml + '<div class="moreOperation">';
		    			chtml = chtml + '<p><a href="editpic.html?id=' + json.id + '" >编辑</a></p>';
		    			chtml = chtml + '<p onclick="removepic(\'' + json.pid + '\');">删除</p>';
		    			chtml = chtml + '<p class="prtsBack">取消</p>';
		    			chtml = chtml + '</div>';
		    			chtml = chtml + '</div>';
		    			
	                    $("#piclist").prepend(chtml);
	                    $('#addpicid').modal('hide');
	                    $("#description").val("");
	                    $("#logoimg").attr("src","http://placehold.it/200x200");
	                    $(".modal-body input").val("");
	                } else {
	                    alertMessage("添加产品错误:[" + json.error_description + "]");
	                }
	            }
	        });
	    }
	    function removepic(pid) {
	        var r = confirm("请确认是否删除图片?")
	        if (r === true)
	        {
	            $.ajax({
	                type: "post",
	                url: getServerUrl() + "/Open/Company/delpic",
	                dataType: 'json',
	                data:"access_token="+getToken()+ '&id=' + pid,
	                success: function (json) {
	                    if (json.result == "1") {
	                        alertMessage("删除图片成功！");
	                        loadPic();
	                    } else {
	                        alertMessage("删除产品错误:[" + json.error_description + "]");
	                    }
	                }
	            });
	        }
	
	    }
	    
	    function ajaxFileUpload()
	    {
	        $("#loading")
	                .ajaxStart(function () {
	                    $(this).show();
	                })
	                .ajaxComplete(function () {
	                    $(this).hide();
	                });
	        var groupname = $("#groupname").val();
	        $.ajaxFileUpload
	        (
	                {
	                    url: '/Open/Common/uploadpic',
	                    secureuri: false,
	                    fileElementId: 'file',
	                    dataType: 'json',
	                    data: { access_token:getToken(), ajax: '1', groupname: groupname, updFileName: 'file'},
	                    success: function (data)
	                    {
	
	                        if (data.data) {
	                            //$('#logoimg').attr('src','/Public/upload/pics/'+data.data);
	                            //$('#pic').val('/Public/upload/pics/'+data.data);
	                            $('#logoimg').attr('src', data.data);
	                            $('#pic_url').val(data.data);
	                        }
	                        //alert(data.data);
	                    },
	                    error: function (data, status, e)
	                    {
	                        alert(data.info);
	                    }
	                }
	        )
	
	        return false;
	    }
	</script>
</html>
